package com.shop.cereshop.admin.service.feedback.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.admin.dao.feedback.FeedbackDAO;
import com.shop.cereshop.admin.page.feedback.FeedbackVO;
import com.shop.cereshop.admin.param.feedback.GetFeedbackDTO;
import com.shop.cereshop.admin.param.feedback.ProcessFeedbackDTO;
import com.shop.cereshop.admin.service.feedback.FeedbackService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.FeedbackConstant;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.feedback.Feedback;
import com.shop.cereshop.commons.exception.CoBusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: TODO
 * @author lx
 * @date 2025/10/26 11:07
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
public class FeedbackServiceImpl extends ServiceImpl<FeedbackDAO, Feedback> implements FeedbackService {

  private final FeedbackDAO feedbackDAO;

  @Override
  public void processFeedback(ProcessFeedbackDTO param) throws CoBusinessException {
    // 更新反馈状态
    Feedback feedback = new Feedback();
    feedback.setFeedbackId(param.getFeedbackId());
    feedback.setState(param.getState());
    feedback.setUpdateTime(LocalDateTime.now());

    // 更新反馈记录
    feedbackDAO.updateById(feedback);
  }

  @Override
  public Page<FeedbackVO> getList(GetFeedbackDTO param) throws CoBusinessException {
    // 根据不同用户类型查询不同用户反馈列表
    if (FeedbackConstant.UserType.USER.equals(param.getUserType())) {
      // 普通用户
      return getUserFeedbackList(param);
    } else if (FeedbackConstant.UserType.SHOP.equals(param.getUserType())) {
      // 店铺
      return getShopFeedbackList(param);
    } else if (FeedbackConstant.UserType.AGENT.equals(param.getUserType())) {
      // todo: 代理商反馈暂未实现
      // 代理商
      return getAgentFeedbackList(param);
    } else if (FeedbackConstant.UserType.SALES_PERSION.equals(param.getUserType())) {
      // todo: 业务员反馈暂未实现
      // 业务员
      return getSalesPersonFeedbackList(param);
    } else if (FeedbackConstant.UserType.SHAREHOLDER.equals(param.getUserType())) {
      // todo: 股东反馈暂未实现
      // 股东
      return getShareholderFeedbackList(param);
    } else if (FeedbackConstant.UserType.ANONYMOUS_USER.equals(param.getUserType())) {
      // todo: 匿名用户反馈暂未考虑
      // 匿名人员
      return getAnonymousFeedbackList(param);
    }
    throw new CoBusinessException(CoReturnFormat.PARAM_INVALID);
  }

  @Override
  public FeedbackVO getDetail(Long feedbackId, Integer userType) throws CoBusinessException {
    // 获取反馈详情
    // 判断用户类型
    if (FeedbackConstant.UserType.USER.equals(userType)) {
      return feedbackDAO.getUserFeedbackDetail(feedbackId);
    } else if (FeedbackConstant.UserType.SHOP.equals(userType)) {
      return feedbackDAO.getShopFeedbackDetail(feedbackId);
    } else if (FeedbackConstant.UserType.AGENT.equals(userType)) {
      return feedbackDAO.getAgentFeedbackDetail(feedbackId);
    } else if (FeedbackConstant.UserType.SALES_PERSION.equals(userType)) {
      return feedbackDAO.getSalesPersonFeedbackDetail(feedbackId);
    } else if (FeedbackConstant.UserType.SHAREHOLDER.equals(userType)) {
      return feedbackDAO.getShareholderFeedbackDetail(feedbackId);
    } else if (FeedbackConstant.UserType.ANONYMOUS_USER.equals(userType)) {
      return feedbackDAO.getAnonymousFeedbackDetail(feedbackId);
    }
    throw new CoBusinessException(CoReturnFormat.PARAM_INVALID);
  }

  private Page<FeedbackVO> getShareholderFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getShareholderFeedbackList(param);
    return null;
  }

  private Page<FeedbackVO> getAnonymousFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getAnonymousFeedbackList(param);
    return null;
  }

  private Page<FeedbackVO> getSalesPersonFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getSalesPersonFeedbackList(param);
    return null;
  }

  private Page<FeedbackVO> getAgentFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getAgentFeedbackList(param);
    return null;
  }

  private Page<FeedbackVO> getShopFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getShopFeedbackList(param);
    PageInfo<FeedbackVO> pageInfo = new PageInfo<>(list);
    return new Page<>(pageInfo.getList(), pageInfo.getTotal());
  }

  private Page<FeedbackVO> getUserFeedbackList(GetFeedbackDTO param) {
    // 创建分页参数
    PageHelper.startPage(param.getPage(), param.getPageSize());
    List<FeedbackVO> list = feedbackDAO.getUserFeedbackList(param);
    PageInfo<FeedbackVO> pageInfo = new PageInfo<>(list);
    return new Page<>(pageInfo.getList(), pageInfo.getTotal());
  }
}